export enum Text {
    black = '90',
    red = '91',
    green = '92',
    yellow = '93',
    blue = '94',
    white = '37'
}
export enum Back {
    black = '100',
    red = '101',
    green = '102',
    yellow = '103',
    blue = '104',
    white = '107'
}
export enum Style {
    bold = '1', //加粗
    fade = '2', //暗淡
    line = '4', //下画线
    blink = '5' //闪烁
}

export type PrintType = Text | Back | Style

export const t = function (text: string, ...styles: PrintType[]) {
    if (Array.isArray(styles) && styles.length) {
        const pre = process.stdout.isTTY && process.platform === 'win32' ? '\x1b' : '\u001b'
        const styleCoeds = styles.map((item) => {
            return `${pre}[${item}m`
        })
        const reset = `${pre}[0m`
        return `${styleCoeds.join('')}${text}${reset}`
    }
    return text
}

export const print = function (text: string, ...styles: PrintType[]) {
    process.stdout.write(t(text, ...(styles || [Text.white])))
}
export const printLog = function (...messages: any[]) {
    const info = messages.map((m) => String(m)).join(' ') + '\n'
    process.stdout.write(info)
}

export const printInfo = (...messages: any[]) => {
    const info = messages.map((m) => String(m)).join(' ') + '\n'
    process.stdout.write(t(`[INFO] ${info}`, Text.white))
}
export const printSuccess = (...messages: any[]) => {
    const info = messages.map((m) => String(m)).join(' ') + '\n'
    process.stdout.write(t(`${info}`, Text.green))
}
export const printWarn = (...messages: any[]) => {
    const info = messages.map((m) => String(m)).join(' ') + '\n'
    process.stdout.write(t(`[WARN] ${info}`, Text.yellow))
}

export const printError = (...messages: any[]) => {
    const info = messages.map((m) => String(m)).join(' ') + '\n'
    process.stdout.write(t(`[ERROR] ${info}`, Text.red))
}

export const clearLine = () => {
    const tty = process.stdout.isTTY && !process.env.CI
    if (tty) {
        process.stdout.clearLine(0)
        process.stdout.cursorTo(0)
    }
}
